
package com.probiz.estoresa.customer.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.customer.Membership;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.exception.ApplicationException;
import com.probiz.estore.customer.service.MembershipManager;

public class MembershipController extends GenericStoreAdminController<Membership> {
	private MembershipManager	membershipManager	= null;

	public void setMembershipManager(MembershipManager membershipManager) {
		this.membershipManager = membershipManager;
	}
	
	@Override
	protected String getEntityName(Membership entity) {
		return entity.getMembershipName();
	}

	@Override
	protected void initController() throws Exception {
		this.mgr = membershipManager;
	}
	
	@Override
	protected Map<String,String> getSortMap(){
		Map<String,String> sortMap = new HashMap<String,String>();
		sortMap.put("1", "convert_gbk(membershipName)");
		sortMap.put("2", "membershipLevel");
		sortMap.put("3", "upgradeShopPoint");
		return sortMap;
	}

	@Override
	protected void preSave(HttpServletRequest request, Membership entity, BindException errors) {
	}
	
	
	@Override
	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 取得Form对应的Model
		Membership entity = formBackingObject(request);
		BindException errors = null;
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			ServletRequestDataBinder binder = bindAndValidate(request, entity);
			errors = new BindException(binder.getBindingResult());
			// 传给子类的实现，后者可以继续验证和加入错误到errors
			preSave(request, entity, errors);
			if (!errors.hasErrors()) {// 里面如果出错应该抛出异常
				Map<String, Object> saveData = membershipManager.saveFully(entity);
				data.putAll(saveData);
				if(data.containsKey("error")){
					saveErrorMessage(request, (String)data.get("error"));
				}else{
					postSave(request, entity);
				}
			}
		} catch (ApplicationException e) {
			handleApplicationException(errors, e);
		}
		
		return showForm(request, errors);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.core.controller.GenericStoreAdminController#multiDelete(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ModelAndView multiDelete(HttpServletRequest request,
			HttpServletResponse response) {
		String[] pkeys = request.getParameterValues("multiIds");
		if (pkeys != null) {
			for (int i = 0; i < pkeys.length; i++) {
				if (StringUtils.isNotEmpty(pkeys[i])) {
					Membership membership = membershipManager.getById(new Integer(pkeys[i]));
					Map<String, Object> data = membershipManager.deleteFully(membership);
					if(data.containsKey("error")){
						saveErrorMessage(request, (String)data.get("error"));
					}else{
						String message = getMessage("membership.deleted", new Object[] { membership.getMembershipName() });
						saveMessage(request, message);
					}
				}
			}
		}

		return this.getRedirectToActionView("search");
	}

}
